<?php

require_once('../../../../wp-config.php');
global $wpdb;

header_remove('Access-Control-Allow-Origin');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token , Authorization');

$_POST = json_decode(file_get_contents('php://input'), true);


/* API for the admin login */
if (isset($_POST['action']) && $_POST['action'] == 'user_login') {
	$creds = array();
	$creds['user_login'] = $_POST['username'];
	$creds['user_password'] = $_POST['password'];
	$creds['remember'] = true;
	$user = wp_signon($creds, false);

	if (is_wp_error($user)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Please check your username and password"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $user];
		echo wp_send_json($valid, 200);
	}
}

/* API for the list of quotation */
if (isset($_POST['action']) && $_POST['action'] == 'get_quotation_list') {
	$result = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "mp_bookings order by lastmodify desc");
	if (empty($result)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$arr  = array();

		foreach ($result as $value) {


			$value->source_city = $value->source_city;
			if ($value->via_city) {
				$value->via_city = $value->via_city;
			} else {
				$value->via_city = "";
			}

			$value->destination_city = $value->destination_city;
			array_push($arr, $value);
		}

		$valid = ['status' => "true", "statuscode" => 200, 'response' => $arr];
		echo wp_send_json($valid, 200);
	}
}



/* API for the quotation details for the single quote */
if (isset($_POST['action']) && $_POST['action'] == 'get_quotation_detail') {
	$order_id = $_POST['order_id'];
	$result = $wpdb->get_results("SELECT * FROM  " . $wpdb->prefix . "mp_bookings 	where order_id=" . $order_id);
	if (empty($result)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$result[0]->source_city = $result[0]->source_city;
		if ($result[0]->via_city) {
			$result[0]->via_city = $result[0]->via_city;
		} else {
			$result[0]->via_city = "";
		}
		$result[0]->destination_city = $result[0]->destination_city;

		$valid = ['status' => "true", "statuscode" => 200, 'response' => $result];
		echo wp_send_json($valid, 200);
	}
}

/* API for the send quote reply */
if (isset($_POST['action']) && $_POST['action'] == 'send_quote') {
	$quote_price = $_POST['quote_price'];
	$quote_details = $_POST['quote_comment'];
	$id = $_POST['id'];
	$queryString = "UPDATE  " . $wpdb->prefix . "mp_bookings  SET quote_price='" . $quote_price . "',quote_detail='" . $quote_detail . "' WHERE id=" . $id;
	$result = $wpdb->query($queryString);

	if (is_wp_error($user)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Something is wrong"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => "Reply sent to customer"];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user sign-in */
if (isset($_POST['action']) && $_POST['action'] == 'customer_signup') {

	$creds = array();
	$creds['name'] = $_POST['name'];
	$creds['phone'] = $_POST['phone'];
	$creds['email'] = $_POST['email'];
	$creds['password'] = $_POST['password'];
	$creds['confirm_password'] = $_POST['confirm_password'];
	$date = date("Y-m-d H:i:s");

	$check = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "users WHERE user_email = '" . $creds['email'] . "'");

	if (empty($check)) {

		if ($creds['confirm_password'] == $creds['password']) {

			$insert_wp_user = $wpdb->query("INSERT INTO " . $wpdb->prefix . "users (`user_login`,`user_pass`, `user_nicename`, `user_email`, `user_registered`,`display_name`) VALUES ('" . $creds['name'] . "','" . md5($creds['password']) . "','" . $creds['name'] . "','" . $creds['email'] . "','" . $date . "','" . $creds['name'] . "')");
		} else {
			$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Something is wrong"];
			echo wp_send_json($invalid, 404);
		}

		$last_id_inserted = $wpdb->insert_id;

		add_user_meta($last_id_inserted, 'phone', $creds['phone']);


		$user = new WP_User($last_id_inserted);
		$user->set_role('subscriber');
	} else {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "This email is already exist"];
		echo wp_send_json($invalid, 404);
	}
}

/* API for the user move-size */
if (isset($_POST['action']) && $_POST['action'] == 'get_movesize') {

	$queryString = "SELECT * FROM " . $wpdb->prefix . 'mp_movesize' . " ORDER BY position ASC";
	$move_size_arr = $wpdb->get_results($queryString);

	if (empty($move_size_arr)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $move_size_arr];
		echo wp_send_json($valid, 200);
	}
}
/* API for the user get_room_type_front */
if (isset($_POST['action']) && $_POST['action'] == 'get_room_type_front') {

	$queryString1 = "SELECT * FROM " . $wpdb->prefix . 'mp_room_type' . "  ORDER BY  id ASC";
	$get_room_type_front = $wpdb->get_results($queryString1);

	if (empty($get_room_type_front)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_room_type_front];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_article_category */
if (isset($_POST['action']) && $_POST['action'] == 'get_article_category') {

	$queryString2 = "SELECT * FROM " . $wpdb->prefix . 'mp_article_category' . " ORDER BY  room_type_id ASC";
	$get_article_category = $wpdb->get_results($queryString2);
	if (empty($get_article_category)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_article_category];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_all_article */
if (isset($_POST['action']) && $_POST['action'] == 'get_all_article') {

	$queryString2 = "SELECT * FROM " . $wpdb->prefix . 'mp_articles' . " WHERE status='E' ";
	$get_all_article = $wpdb->get_results($queryString2);
	if (empty($get_all_article)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_all_article];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_floors */
if (isset($_POST['action']) && $_POST['action'] == 'get_floors') {

	$queryString2 = "SELECT * FROM " . $wpdb->prefix . "mp_floors";
	$get_floors = $wpdb->get_results($queryString2);
	if (empty($get_floors)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_floors];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user update_profile */
if (isset($_POST['action']) && $_POST['action'] == 'update_profile') {
	$creds = array();
	$creds['name'] = $_POST['name'];
	$creds['email'] = $_POST['email'];
	$phone = $_POST['phone'];
	$user_id = $_POST['user_id'];
	

	$metas = array( 
		'phone'   => $phone
		
	);
	
	foreach($metas as $key => $value) {
		update_user_meta( $user_id, $key, $value );
	}
	
	$queryString2 = "UPDATE " . $wpdb->prefix . "users SET `user_login` = '" . $creds['name'] . "',`user_email` = '" . $creds['email'] . "' WHERE ID =" . $user_id;
	
	$update_profile2 = $wpdb->query($queryString2);

	if (empty($update_profile2)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $update_profile2];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user change_password */
if (isset($_POST['action']) && $_POST['action'] == 'change_password') {
	$current_user     = $_POST['user_id'];
    $current_password = $_POST['old_pass'];
    $new_password     = $_POST['new_pass'];
    $confirm_password = $_POST['confirm_password'];
	$check = $wpdb->get_results("SELECT `user_pass` FROM " . $wpdb->prefix . "users WHERE ID = '" . $current_user . "'");
    $result = wp_check_password( $current_password, $check[0]->user_pass, $current_user );

    if ( $result ) {
        if ( $new_password == $confirm_password ) {
            wp_set_password( $new_password, $current_user );
            wp_set_auth_cookie ( $current_user );
            wp_set_current_user( $current_user );
            do_action('wp_login', $user->user_login, $user );
            echo 'Your new password is changed.';
        } else {
            echo 'Your current password is correct, but the new and confirm passwords do not match.';
        }
    } else {
        echo 'Your current password is incorrect.';
    }

    
	
	
}


/* API for the user get_additional_info */
if (isset($_POST['action']) && $_POST['action'] == 'get_additional_info') {

	$queryString4 = "SELECT *FROM  " . $wpdb->prefix . "mp_additional_info";
	$get_additional_info = $wpdb->get_results($queryString4);

	if (empty($get_additional_info)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_additional_info];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_elevator */
if (isset($_POST['action']) && $_POST['action'] == 'get_elevator') {

	$queryString4 = "SELECT * FROM " . $wpdb->prefix . "mp_elevator";
	$get_elevator = $wpdb->get_results($queryString4);

	if (empty($get_elevator)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_elevator];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_packaging_unpackaging */
if (isset($_POST['action']) && $_POST['action'] == 'get_packaging_unpackaging') {

	$queryString4 = "SELECT * FROM " . $wpdb->prefix . "mp_packaging_unpackagin";
	$get_packaging_unpackaging = $wpdb->get_results($queryString4);;

	if (empty($get_packaging_unpackaging)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_packaging_unpackaging];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_cubicfeets */
if (isset($_POST['action']) && $_POST['action'] == 'get_cubicfeets') {

	$queryString4 = "SELECT * FROM " . $wpdb->prefix . "mp_cubicfeets";
	$get_cubicfeets = $wpdb->get_results($queryString4);;

	if (empty($get_cubicfeets)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_cubicfeets];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_customer_booking_list */
if (isset($_POST['action']) && $_POST['action'] == 'get_customer_booking_list') {

	$queryString4 = "SELECT * FROM  " . $wpdb->prefix . "mp_bookings";
	$get_customer_booking_list = $wpdb->get_results($queryString4);

	if (empty($get_customer_booking_list)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_customer_booking_list];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user get_quotation_list1 */
if (isset($_POST['action']) && $_POST['action'] == 'get_quotation_list1') {

	$queryString4 = "SELECT `quote_price`,`quote_detail` FROM  " . $wpdb->prefix . "mp_bookings  ORDER BY  id ASC";
	$get_quotation_list1 = $wpdb->get_results($queryString4);;

	if (empty($get_quotation_list1)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_quotation_list1];
		echo wp_send_json($valid, 200);
	}
}


/* API for the user get_distance_unit */
if (isset($_POST['action']) && $_POST['action'] == 'get_distance_unit') {

	$queryString4 = "SELECT * FROM  " . $wpdb->prefix . "mp_distance";
	$get_distance_unit = $wpdb->get_results($queryString4);

	if (empty($get_distance_unit)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $get_distance_unit];
		echo wp_send_json($valid, 200);
	}
}

/* API for the user bookComplete */
if (isset($_POST['action']) && $_POST['action'] == 'bookComplete') {
	/* First Step Info */
	
	$name = $_POST['name'];
	
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$order_id = $_POST['order_id'];
	
	$source_city = $_POST['source_city'];
	$date_period = $_POST['date_period'];
	$via_city = $_POST['via_city'];
	$destination_city = $_POST['destination_city'];
	$move_size = $_POST['move_size'];
	$booking_date = date('Y-m-d');
	$quote_price = $_POST['quote_price'];
	$quote_detail = $_POST['quote_detail'];
	$manpower = $_POST['manpower'];
	$booking_status = $_POST['booking_status'];
	$payment_method = $_POST['payment_method'];
	$transaction_id = $_POST['transaction_id'];
	$reminder = $_POST['reminder'];
	$notification = $_POST['notification'];
	$lastmodify = date_i18n('Y-m-d H:i:s');
	$insurance_rate = $_POST['insurance_rate'];
	$cubic_feet_volume = $_POST['cubic_feet_volume'];
	$distances = $_POST['distances'];
	$cubicfeet_title = $_POST['cubicfeet_title'];
	$cubicfeet_count = $_POST['cubicfeet_count'];
	$moreid = $_POST['moreid'];

	function getTime() {
		$dd = new Date();
		$hh = $dd.getHours();
	
		if($hh > 11){
		document.write("Good afternoon");
		}else{
		document.write("Good morning");
		}
	
	}
	

	
	$manpower1=serialize($manpower);
	
	/* $service_info = array(); */
	$loading_info = array();
	$unloading_info = array();
	$additional_info = array();
	$user_info = array();
	$cubic_feet_article = array();

	/* $service_info['get_article_room_id'] = $_POST['service_info'][0]['get_article_room_id'];
	$service_info['get_article_cat_id'] = $_POST['service_info'][0]['get_article_cat_id'];
	$service_info['get_article_id'] = $_POST['service_info'][0]['get_article_id'];
	$service_info['get_article_name'] = $_POST['service_info'][0]['get_article_name'];
	$service_info['count'] = $_POST['service_info'][0]['count'];
	$service_info['per_quibcfeets_volume'] = $_POST['service_info'][0]['per_quibcfeets_volume'];
	$service_info['article_ass_dess'] = $_POST['service_info'][0]['article_ass_dess']; */
	
	
  	$loading_info['loading_floor_amt'] = $_POST['loading_info'][0]['loading_floor_amt'];
	$loading_info['loading_floor_number'] = $_POST['loading_info'][0]['loading_floor_number'];
	$loading_info['load_elevator_amt'] = $_POST['loading_info'][0]['load_elevator_amt'];
	$loading_info['load_elevator_checked'] = $_POST['loading_info'][0]['load_elevator_checked'];
	$loading_info['packaging_amt'] = $_POST['loading_info'][0]['packaging_amt'];
	$loading_info['packaging_checked'] = $_POST['loading_info'][0]['packaging_checked'];
	$loading_info['loading_address_one'] = $_POST['loading_info'][0]['loading_address_one'];
	$loading_info['loading_address_two'] = $_POST['loading_info'][0]['loading_address_two'];
	$loading_info['loading_address_three'] = $_POST['loading_info'][0]['loading_address_three'];
	
	$unloading_info['unloading_floor_amt'] = $_POST['unloading_info'][0]['unloading_floor_amt'];
	$unloading_info['unloading_floor_number'] = $_POST['unloading_info'][0]['unloading_floor_number'];
	$unloading_info['unload_elevator_amt'] = $_POST['unloading_info'][0]['unload_elevator_amt'];
	$unloading_info['unload_elevator_checked'] = $_POST['unloading_info'][0]['unload_elevator_checked'];
	$unloading_info['unpackaging_amt'] = $_POST['unloading_info'][0]['unpackaging_amt'];
	$unloading_info['unpackaging_checked'] = $_POST['unloading_info'][0]['unpackaging_checked'];
	$unloading_info['unloading_address_one'] = $_POST['unloading_info'][0]['unloading_address_one'];
	$unloading_info['unloading_address_two'] = $_POST['unloading_info'][0]['unloading_address_two'];
	$unloading_info['unloading_address_three'] = $_POST['unloading_info'][0]['unloading_address_three'];

	$additional_info['0'] = $_POST['additional_info'][0]['favorite'][0]['0'];
	$additional_info['1'] = $_POST['additional_info'][0]['favorite'][0]['1'];

	$user_info['name'] = $_POST['user_info'][0]['name'];
	$user_info['email'] = $_POST['user_info'][0]['email'];
	$user_info['phone'] = $_POST['user_info'][0]['phone'];
	$user_info['country_code'] = $_POST['user_info'][0]['country_code'];
	$user_info['cust_password'] = $_POST['user_info'][0]['cust_password'];

	/* $cubic_feet_article['cubicfeet_title'] = $_POST['cubic_feet_article'][0][1]['cubicfeet_title'];
	$cubic_feet_article['cubicfeet_count'] = $_POST['cubic_feet_article'][0]['cubicfeet_count'];
	$cubic_feet_article['moreid'] = $_POST['cubic_feet_article'][0]['moreid'];
	$cubic_feet_article['cubicfeet_title'] = $_POST['cubic_feet_article'][2]['cubicfeet_title'];
	$cubic_feet_article['cubicfeet_count'] = $_POST['cubic_feet_article'][2]['cubicfeet_count'];
	$cubic_feet_article['moreid'] = $_POST['cubic_feet_article'][2]['moreid']; */
	
	
	$cubic_feet_article = array( 
		'cubicfeet_title'   => $cubicfeet_title,
		'cubicfeet_count'   => $cubicfeet_count,
		'moreid'   => $moreid
		
	);
	
	foreach($cubic_feet_article as $key => $value) {
		array($client_id, $key, $value );
		
	}
	
	$service_info1=serialize($_POST['service_info']);
	$loading_info1=serialize($loading_info);
	$unloading_info2=serialize($unloading_info);
	$additional_info1=serialize($additional_info);
	$user_info1=serialize($user_info);
	$cubic_feet_article1=serialize($cubic_feet_article);

	
	/* print_r("INSERT INTO ". $wpdb->prefix ."mp_bookings (`id`,`order_id`, `client_id`, `source_city`, `via_city`, `destination_city`,`move_size`, `booking_date`, `date_period`, `quote_price`, `quote_detail`, `service_info`, `loading_info`, `unloading_info`,`additional_info`, `manpower_info`, `user_info`, `booking_status`, `payment_method`, `transaction_id`, `reminder`, `notification`,`lastmodify`, `insurance_rate`, `cubic_feets_article`, `cubic_feets_volume`, `distances`) VALUES ('$id','$order_id','$client_id','$source_city','$via_city','$destination_city','$move_size','$booking_date','$date_period','$quote_price','$quote_detail','$service_info1','$loading_info1','$unloading_info2','$additional_info1','$manpower1','$user_info1','$booking_status','$payment_method','$transaction_id','$reminder','$notification','$lastmodify','$insurance_rate','$cubic_feet_article1','$cubic_feet_volume','$distances')");
	die; */
	$mp_booking = $wpdb->query("INSERT INTO ". $wpdb->prefix ."mp_bookings (`id`,`order_id`, `client_id`, `source_city`, `via_city`, `destination_city`,`move_size`, `booking_date`, `date_period`, `quote_price`, `quote_detail`, `service_info`, `loading_info`, `unloading_info`,`additional_info`, `manpower_info`, `user_info`, `booking_status`, `payment_method`, `transaction_id`, `reminder`, `notification`,`lastmodify`, `insurance_rate`, `cubic_feets_article`, `cubic_feets_volume`, `distances`) VALUES ('','$order_id','','$source_city','$via_city','$destination_city','$move_size','$booking_date','$date_period','$quote_price','$quote_detail','$service_info1','$loading_info1','$unloading_info2','$additional_info1','$manpower1','$user_info1','$booking_status','$payment_method','$transaction_id','$reminder','$notification','$lastmodify','$insurance_rate','$cubic_feet_article1','$cubic_feet_volume','$distances')");
	
	$queryString ="INSERT INTO ".$wpdb->prefix."mp_order_client_info (`id`, `order_id`, `client_name`, `client_email`, `client_phone`, `client_personal_info`) VALUES ('$id', '$order_id', '".$user_info['name']."', '".$user_info['email']."', '".$user_info['phone']."', '')";

	$insert_wp_user = $wpdb->query("INSERT INTO " . $wpdb->prefix . "users (`user_login`,`user_pass`, `user_nicename`, `user_email`, `user_registered`,`display_name`) VALUES ('" . $user_info['name'] . "','" . md5($user_info['cust_password']) . "','" . $user_info['name'] . "','" . $user_info['email'] . "','" . $date . "','" . $user_info['name'] . "')");

	if($mp_booking){
		$valid = ['status' => "true", "statuscode" => 200, 'response' =>$mp_booking];
		echo wp_send_json($valid, 200);
	}else{
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	}
}

/* API for the user get_settings */
if (isset($_POST['action']) && $_POST['action'] == 'get_settings') {
	$option_name = $_POST['option_name'];
	
	$queryString7 = "SELECT * FROM  " . $wpdb->prefix . "options";
	$get_settings = $wpdb->get_results($queryString7);
 	/* print_r($get_settings);
	die;  */
	
	$i=0;
	foreach($get_settings as $row) {
		if($option_name == $row->option_name)
		{
			$i++;
			break; 
		}
				
		
	}
	
	
	if (empty($row)) {
		$invalid = ['status' => "false", "statuscode" => 404, 'response' => "Data not found"];
		echo wp_send_json($invalid, 404);
	} else {
		$valid = ['status' => "true", "statuscode" => 200, 'response' => $row];
		echo wp_send_json($valid, 200);
	}
}